//
//  CSYHomeTableViewCell.swift
//  CSYMicroBlockSina
//
//  Created by 姚彦兆 on 15/11/14.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit
import SnapKit
import FFLabel

let cellMargin : CGFloat = 12

let iconWidth : CGFloat = 35

class CSYHomeTableViewCell: UITableViewCell {

    private var bottomConstraint : Constraint?
    
    var blogModel : CSYMyMicroBlogModel? {
    
        didSet{
        
            originView.messageModel = blogModel
            
            self.bottomConstraint?.uninstall()
            
            if let rModel = blogModel?.retweetedMessageModel{
            
                retweetedView.retweetedModel = rModel
                
                retweetedView.hidden = false
                
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraint = make.top.equalTo(retweetedView.snp_bottom).offset(1).constraint
                    
                })
                
            }else{
            
                retweetedView.hidden = true
            
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraint = make.top.equalTo(originView.snp_bottom).offset(1).constraint
                    
                })
                
            }
        
        }
    
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .None
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    private func setUpUI(){
        
        let separateView : UIView = getSeparateView()
        
        contentView.addSubview(separateView)
        
        separateView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(contentView.snp_left)
            
            make.right.equalTo(contentView.snp_right)
            
            make.height.equalTo(10)
            
            make.top.equalTo(contentView.snp_top).offset(1)
            
        }
        
        let headView : UIView = getHeaderView()
        
        contentView.addSubview(headView)
        
        headView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(separateView.snp_bottom).offset(1)
            
            make.centerX.equalTo(contentView.snp_centerX)
            
            make.width.equalTo(snp_width).offset(-10.0)
            
            make.height.equalTo(lineWidth)
            
        }
        
        contentView.addSubview(originView)
        
        originView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(headView.snp_bottom).offset(0)
            
            make.left.equalTo(contentView.snp_left).offset(0)
            
            make.right.equalTo(contentView.snp_right).offset(0)
            
        }
        
        contentView.addSubview(retweetedView)
        
        retweetedView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(originView.snp_bottom)
            
            make.left.equalTo(contentView.snp_left)
            
            make.right.equalTo(contentView.snp_right)
            
        }
        
        contentView.addSubview(bottomView)
        
        bottomView.snp_makeConstraints { (make) -> Void in
            
            self.bottomConstraint = make.top.equalTo(retweetedView.snp_bottom).constraint
            
            make.left.equalTo(contentView.snp_left)
            
            make.right.equalTo(contentView.snp_right)
            
            make.height.equalTo(30)
            
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(bottomView.snp_bottom).offset(0)
            
            make.top.equalTo(self.snp_top)
            
            make.left.equalTo(self.snp_left)
            
            make.right.equalTo(self.snp_right)
            
        }
        
    
    }
    
    //懒加载公共子控件
    
    lazy var originView : CSYOriginBlogView = CSYOriginBlogView()

    lazy var bottomView : CSYBottomView = CSYBottomView()
    
    lazy var retweetedView : CSYRetweetedView = CSYRetweetedView()
    
    private func getSeparateView () -> UIView {
    
        let view = UIView()
        
        view.backgroundColor = UIColor(white: 0.97, alpha: 1)
        
        return view
    
    }
    
    private func getHeaderView () -> UIView {
        
        let view = UIView()
        
        view.backgroundColor = UIColor(white: 0.8, alpha: 1)
        
        return view
        
    }
    
}

